package com.imooc.controller;

import com.imooc.domain.Girl;
import com.imooc.repository.GirlRepository;
import com.imooc.result.Result;
import com.imooc.result.ResultUtils;
import com.imooc.service.GirlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by chenxin on 2017/4/24.
 */
@RestController
public class GirlController {
    public final static Logger logger = LoggerFactory.getLogger(GirlController.class);
    //
    @Autowired
    private GirlService girlService;
    @Autowired
    private GirlRepository girlRepository;
    //public final  static Logger
 @GetMapping("/girls")
 //查询女生列表
 public List<Girl> girlList(){
     logger.info("调用方法好时候{}",22222222);
     //System.out.println(2222222);
     return girlRepository.findAll();

 }
    //新增一条数据
    @PostMapping("girls")
    public Result<Girl> girlAdd(@Valid Girl girl , BindingResult bindingResult){
     //判断
        if(bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return null;
        }
        if(bindingResult.hasErrors()){
//            Result result=new Result();
//            result.setCode(2);
//            result.setMessage(bindingResult.getFieldError().getDefaultMessage());
//            Map map =new HashMap();
//            result.setData(map);
//            return result;
            return ResultUtils.error(99999,bindingResult.getFieldError().getDefaultMessage());
        }

        girl.setCupSize(girl.getCupSize());
        girl.setAge(girl.getAge());
//        Result result=new Result();
//        result.setCode(0);
//        result.setMessage("成功");
//        result.setData(girlRepository.save(girl));
//        return result;
        return ResultUtils.success(girlRepository.save(girl));
       //return girlRepository.save(girl);
        //return null;
    }
   //通过ID查询女生
    @GetMapping(value="/girls/{id}")
    public Girl selectOne(@PathVariable("id") Integer id ){
       return girlRepository.findOne(id);
    }
   //更改
  @PutMapping(value = "/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id ,
                           @RequestParam("cupSize") String cupSize,@RequestParam("age") Integer age  ){
   Girl girl=new Girl();
      girl.setCupSize(cupSize);
      girl.setAge(age);
      girl.setId(id);
    return  girlRepository.save(girl);
  }
    //删除
    @DeleteMapping (value="/girls/{id}")
    public void delete(@PathVariable("id") Integer id ){
       girlRepository.delete(id);
    }
    //查询byage
    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> girlselectByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }
    //插入 事物管理

 @PostMapping(value = "/girls/insert")
 public void insertTwo(){
     girlService.InsertTwo();
 }
 @GetMapping(value = "/girls/getage/{id}")
 public void getAge(@PathVariable("id") Integer id) throws Exception {
     girlService.getAgeById(id);
 }
}
